import {setTimeout} from "core-js/internals/schedulers-fix";

let baseURL = process.env.VUE_APP_BASE_URL
const Request = options => {
	// 判断参数发送类型
	let contentType = 'application/json; charset=UTF-8'
	if (options.contentType === 'formData') {
		contentType = 'application/x-www-form-urlencoded; charset=UTF-8'
	}
	// token
	let userInfo = uni.getStorageSync('staff_protocol') || {}
	if (typeof userInfo === "string") {
		userInfo = JSON.parse(userInfo)
	}
	const token = (userInfo) ? userInfo.token : null
	// header配置项
	const headerData = {
		Accept: 'application/json',
		'X-Requested-With': 'XMLHttpRequest',
		'content-type': contentType,
		'token': token
	}
	// 判断请求类型
	return new Promise((resolve, reject) => {
		uni.showLoading({
			title: '加载中···',
			mask: true,
		})
		if (options ? options.contentType === 'fileUpload' : false) {
			fileRequest()
		} else {
			networkRequest()
		}

		// 网络请求
		function networkRequest() {
			uni.request({
				method: options.method,
				url: baseURL + options.url,
				data: options.data,
				header: headerData,
				timeout: 10000,
				responseType: options.responseType,
				success: (response) => {
					if (handleApiResponseStatus(response.data)) resolve(response.data)
				},
				fail: (error) => {
					if (handleHttpResponseStatus(error)) reject(error)
				},
				complete: () => {
					// setTimeout(() => {
					// 	uni.hideLoading()
					// }, 1000)
				},
			})
		}

		// 文件请求

		function fileRequest() {
			uni.uploadFile({
				url: baseURL + options.url,
				filePath: options.data,
				name: options.name,
				header: {
					token: token,
				},
				timeout: 100000,
				success: response => {
					if (response.statusCode === 200) {
						const data = JSON.parse(response.data)
						if (handleApiResponseStatus(data)) resolve(data)
					} else if (response.statusCode === 401) {
						const data = JSON.parse(response.data)
						handleRedirectLogin(data)
						return false
					} else {
						uni.showToast({
							title: response.errMsg || response.message || '网络错误',
							icon: 'none',
							duration: 3000
						})
					}
				},
				fail(error) {
					if (handleHttpResponseStatus(error, options)) reject(error)
				},
				complete: () => {
					// setTimeout(() => {
					// 	uni.hideLoading()
					// }, 3000)
				},
			})
		}
	})
}

// API状态拦截
const handleApiResponseStatus = response => {
	if (response.byteLength || response.code === 0) {
		uni.hideLoading()
		return true
	} else if (response.code === 401) {
		handleRedirectLogin(response)
		return false
	} else {
		uni.showToast({
			title: response.message || response.msg || '系统繁忙请稍后在试',
			icon: 'none',
			duration: 3000
		})
	}
}
// 处理重定向LOGIN
const handleRedirectLogin = (response) => {
	uni.showLoading({
		title: response.message || 'TOKEN过期',
		mask: true,
		success: function() {
			uni.removeStorageSync('staff_protocol')
		}
	})
	setTimeout(() => {
		wx.redirectTo({
			url: '/pages/login/smsLogin',
		})
	}, 1000)
}
// 其它网络异常拦截
const handleHttpResponseStatus = (options) => {
	// 文件上传失败在页面回调
	if (options ? options.contentType === 'fileUpload' : false) {
		uni.hideLoading()
		return true
	} else {
		uni.showToast({
			title: '请求超时，请稍后重试！',
			icon: 'none',
			duration: 3000
		})
		setTimeout(() => {
			uni.hideLoading()
		}, 2000)
		return false
	}
}
export default Request
